package com.sq.util;

import java.io.File;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FilenameUtils;

import com.sq.util.FileUtil;
import com.sq.util.StringUtil;

public class MavenHelperDemo {
    public static void main(String[] args) {
        String dir = "D:\\maven-repository";
        File file = new File(dir);
        clearErrorFile(file.getAbsolutePath(), file);
        // clearOldFile(file.getAbsolutePath(), file);
    }

    public static void clearOldFile(String baseDirAbsolutePath, File file) {
        if (!FileUtil.allSonIsDir(file)) {
            return;
        }
        File[] sonFiles = file.listFiles();
        if (sonFiles.length <= 1) {
            return;
        }
        List<String> nameList = new LinkedList<>();
        for (File sonFile : sonFiles) {
            nameList.add(FilenameUtils.getName(sonFile.getAbsolutePath()));
        }
        List<MavenVerionBO> r = parese(nameList);
        r.sort(new Comparator<MavenVerionBO>() {

            @Override
            public int compare(MavenVerionBO o1, MavenVerionBO o2) {
                if (o1.x == o2.x) {
                    if (o1.y == o2.y) {
                        return Integer.compare(o2.z, o1.z);
                    } else {
                        return Integer.compare(o2.y, o1.y);
                    }
                } else {
                    return Integer.compare(o2.x, o1.x);
                }
            }
        });
        if (r.size() == nameList.size()) {
            System.out.println(file.getAbsolutePath());
            System.out.println(r);
            int i = 0;
            for (MavenVerionBO mavenVerionBO : r) {
                i++;
                if (i < 2) {
                    continue;
                }
                String path = file.getAbsolutePath() + "\\" + mavenVerionBO.name;
                System.out.println("del " + path);
                FileUtil.deleteAllSon(new File(path));
            }
        } else {
            for (File sonFile : sonFiles) {
                if (sonFile.isDirectory()) {
                    clearOldFile(baseDirAbsolutePath, sonFile);
                }
            }
        }
    }

    private static List<MavenVerionBO> parese(List<String> nameList) {
        List<MavenVerionBO> result = new LinkedList<>();
        for (String string : nameList) {
            Pattern p = Pattern.compile("(\\d{1,3})(\\.?)");
            Matcher m = p.matcher(string);
            MavenVerionBO bo = new MavenVerionBO();
            bo.name = string;
            if (m.find()) {
                bo.x = Integer.parseInt(m.group(1));
            }
            if (m.find()) {
                bo.y = Integer.parseInt(m.group(1));
            }
            if (m.find()) {
                bo.z = Integer.parseInt(m.group(1));
            }
            if (bo.x > 0) {
                result.add(bo);
            }
        }
        return result;
    }

    private static void clearErrorFile(String baseDirAbsolutePath, File file) {
        if (file.isFile()) {
            return;
        }
        File[] sonFiles = file.listFiles();
        boolean f = false;
        for (File sonFile : sonFiles) {
            String path = sonFile.getAbsolutePath();
            String name = FilenameUtils.getName(path).toLowerCase();
            if (name.endsWith(".sha1-in-progress")) {
                f = true;
                break;
            }
            if (name.endsWith(".lastupdate")) {
                f = true;
                break;
            }
        }
        if (f) {
            for (File sonFile : sonFiles) {
                String path = sonFile.getAbsolutePath();
                if (StringUtil.startWith(path, baseDirAbsolutePath)) {
                    System.out.println("del " + path);
                    sonFile.delete();
                } else {
                    System.err.println("err " + path);
                }
            }
        } else {
            for (File sonFile : sonFiles) {
                clearErrorFile(baseDirAbsolutePath, sonFile);
            }
        }
    }
}

class MavenVerionBO {
    public int x;
    public int y;
    public int z;
    public String name;

    @Override
    public String toString() {
        return "MavenVerionBO [x=" + x + ", y=" + y + ", z=" + z + ", name=" + name + "]";
    }

}
